# coding:utf-8
"""
@author: jing
@contact: 529548204@qq.com
@file: serializer.py
@time: 2022/5/24 16:56
"""
from rest_framework import serializers
from .models import *


class ModuleSerializers(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    modulename = serializers.CharField(required=True, allow_null=False)
    describe = serializers.CharField(required=False, allow_null=True, allow_blank=True)
    project_id = serializers.IntegerField(required=True, allow_null=False)
    projectname = serializers.SerializerMethodField(read_only=True)

    class Meta(object):
        model = Module
        fields = [
            'id',
            'describe',
            'modulename',
            "project_id",
            'create_time',
            'update_time',
            'projectname',
            'pid',
            'describe'
        ]

    @staticmethod
    def get_projectname(obj):
        project_obj = obj.project
        return project_obj.projectname
